//
//  WmcLocateIssueTipView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/29.
//

import UIKit
import YPBedrock

class WmcLocateIssueTipView: UIView {
    // Lifecycle
    
    override var isHidden: Bool {
        didSet {
            if isHidden {
                if type == .unauthorized {
                    WmcTaskSchedulerManager.resetPauseStatus(type: .locationUnauthorizedTip)
                } else if type == .failure {
                    WmcTaskSchedulerManager.resetPauseStatus(type: .notNetworkLocationFailureTip)
                }
            } else {
                if type == .unauthorized {
                    WmcTrack.addBDAutoTrackEvent("bulletFrameExposure", params: ["name": "无权限弹框"])
                } else if type == .failure {
                    WmcTrack.addBDAutoTrackEvent("bulletFrameExposure", params: ["name": "无网络弹框"])
                }
            }
        }
    }


    convenience init(type: TipType) {
        self.init()
        self.type = type
        self.layer.shadowColor = UIColor.wmc_grayH13_ACAEAF.cgColor
        self.layer.shadowOpacity = 1.0
        self.layer.shadowOffset = CGSize(width: 0, height: 0)
        self.layer.shadowRadius = 4
        
        makeUI()
        bindViewModel()
    }

    // Internal

    enum TipType {
        /// 未授权
        case unauthorized
        /// 定位失败
        case failure
    }

    var clickClosure: ((TipType) -> Void)?
    
    var dismissClosure: (() -> Void)?

    var type: TipType = .unauthorized

//    @discardableResult
//    static func showInView(_ view: UIView, type: TipType, topMargin: CGFloat) -> WmcLocateIssueTipView {
//        let tipView = WmcLocateIssueTipView(type: type)
//        view.addSubview(tipView)
//
//        tipView.snp.makeConstraints { make in
//            make.top.equalTo(topMargin)
//            make.left.equalTo(16)
//            make.right.equalTo(-16)
//            make.height.equalTo(96)
//        }
//
//        return tipView
//    }

    func makeUI() {
        backgroundColor = .wmc_white_FFFFFF
        layer.cornerRadius = 8

        addSubview(iconImageView)
        addSubview(tipLabel)
        addSubview(ignoreBtn)
        addSubview(otherBtn)

        iconImageView.snp.makeConstraints { make in
            make.left.top.equalTo(12)
            make.width.height.equalTo(24)
        }

        tipLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(4)
            make.top.equalTo(iconImageView)
            make.right.equalTo(-16)
        }

        ignoreBtn.snp.makeConstraints { make in
            make.width.equalTo(68)
            make.height.equalTo(32)
            make.centerY.equalTo(otherBtn.snp.centerY)
            make.right.equalTo(otherBtn.snp.left).offset(-4)
        }

        var width: CGFloat = 0.0
        if type == .unauthorized {
            width = 84
        } else if type == .failure {
            width = 60
        }

        otherBtn.snp.makeConstraints { make in
            make.width.equalTo(width)
            make.height.equalTo(32)
            make.bottom.equalTo(-12)
            make.right.equalTo(-16)
        }
        
        if type == .unauthorized {
            tipLabel.text = "地理位置权限说明\n\n在您使用添加含有地理位置水印的功能时，需要您同意授权地理位置权限"
            otherBtn.setTitle("同意", for: .normal)

            otherBtn.snp.updateConstraints { make in
                make.width.equalTo(84)
            }
        } else if type == .failure {
            tipLabel.text = "无法获取地址，请检查网络连接后重试"
            otherBtn.setTitle("重试", for: .normal)

            otherBtn.snp.updateConstraints { make in
                make.width.equalTo(60)
            }
        }
    }

    func bindViewModel() {
        ignoreBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        otherBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.clickClosure?(weakSelf.type)
        }
    }

    func dismiss() {
        isHidden = true
        dismissClosure?()
        dismissClosure = nil
    }

    // Private

    private lazy var iconImageView: UIImageView = {
        let view = UIImageView(image: "location_solid_icon".wmc_image)
        return view
    }()

    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.font = 16.wmc_font
        label.textColor = .wmc_grayH2_323233
        label.numberOfLines = 0
        if type == .unauthorized {
            label.text = "无法获取地点，请允许位置权限"
        } else if type == .failure {
            label.text = "无法获取地址，请检查网络连接后重试"
        }
        return label
    }()

    private lazy var ignoreBtn: UIButton = {
        let btn = UIButton(type: .custom)
        if type == .unauthorized {
            btn.setTitle("拒绝", for: .normal)
        } else if type == .failure {
            btn.setTitle("忽略", for: .normal)
        }
        btn.setTitleColor(.wmc_grayH2_323233, for: .normal)
        btn.titleLabel?.font = 14.wmc_font
        return btn
    }()

    private lazy var otherBtn: UIButton = {
        let btn = UIButton(type: .custom)
        if type == .unauthorized {
            btn.setTitle("前往设置", for: .normal)
        } else if type == .failure {
            btn.setTitle("重试", for: .normal)
        }
        btn.setTitleColor(.wmc_white_FFFFFF, for: .normal)
        btn.setBackgroundImage(UIColor.wmc_mainTheme.yp.uiImage, for: .normal)
        btn.titleLabel?.font = 14.wmc_font
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        return btn
    }()
}
